package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gg.header.getHeaderUser;
import com.gg.user.gguser.dao.GgBusinessOpportunityDaoMapper;
import com.gg.user.gguser.dao.GgInformationDaoMapper;
import com.gg.user.gguser.dao.GgSigningDaoMapper;
import com.gg.user.gguser.dataobject.GgBusinessOpportunityDtaobject;
import com.gg.user.gguser.dataobject.GgInformationDataobject;
import com.gg.user.gguser.dataobject.GgItemsOnlyDataobject;
import com.gg.user.gguser.dataobject.GgSigningDataobject;
import com.gg.user.gguser.service.GgDeletcInformationService;
import com.gg.user.gguser.service.GgItemsOnlyService;
import com.gg.user.gguser.service.GgSigningService;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GgSigningServiceImpl implements GgSigningService {

    @Autowired
    private GgInformationDaoMapper ggInformationDaoMapper;

    @Autowired
    private GgBusinessOpportunityDaoMapper ggBusinessOpportunityDaoMapper;

    @Autowired
    private GgSigningDaoMapper ggSigningDaoMapper;

    @Autowired
    private GgDeletcInformationService ggDeletcInformationService;

    @Autowired
    private GgItemsOnlyService ggItemsOnlyService;

    /**
     * 增加 从信息、商机 新增过来的
     *
     * id 移动过来的id
     * type 从哪里移动过来
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object addSignings(HttpServletResponse response, HttpServletRequest request, Integer id, Integer type) throws Exception {
        if(id != null && id > 0 && type != null) {
            // type 为1的时候是从信息来的
            if(type==1){
                // 查出信息详情
                GgInformationDataobject ggInformationDataobject = ggInformationDaoMapper.getGgInformationDataobjectItems(id);
                if(ggInformationDataobject==null){
                    return return_format.return_format_error(100003,"没有这个信息哦！");
                }else {
                    try {
                        // 获取用户信息
                        // 从cookice信息中过去用户信息 在这里只用要id就好
                        String cookiceUser = getHeaderUser.getHader(response, request, "gg");
                        String ggId = JSONObject.parseObject(cookiceUser).getString("ggId");
                        // 插入签约
                        ggSigningDaoMapper.addSignings(ggInformationDataobject.getGgItemsToken(), ggInformationDataobject.getGgIName(), ggInformationDataobject.getGgIProvince(), ggInformationDataobject.getGgICity(), ggInformationDataobject.getGgIDetailed(), ggInformationDataobject.getGgIHandle(), ggInformationDataobject.getGgIContact(), ggInformationDataobject.getGgIState(), ggInformationDataobject.getGgILevel(), ggInformationDataobject.getGgIGender(), ggInformationDataobject.getGgIPosition(), ggInformationDataobject.getGgITelephone(), ggInformationDataobject.getGgIFax(), ggInformationDataobject.getGgIMailbox(), ggInformationDataobject.getGgIQq(), ggInformationDataobject.getGgIWechat(), ggInformationDataobject.getGgIRemarks(), ggInformationDataobject.getGgId(), ggId, ggInformationDataobject.getGgITime(),ggInformationDataobject.getGgIAreaid());
                        // 从信息里面删除
                        ggInformationDaoMapper.delectGgInformationDataobjectItems(id);
                        return return_format.return_format_All(0,"","");
                    }catch (Exception e){
                        return return_format.return_format_error(100002,"服务有问题了!");
                    }
                }
            }else if(type==2){ // type 为1的时候是从商机来的
                GgBusinessOpportunityDtaobject ggBusinessOpportunityDtaobject = ggBusinessOpportunityDaoMapper.getBusinessOpportunity(id);// 获取商机信息
                if(ggBusinessOpportunityDtaobject==null){
                    return return_format.return_format_error(100003,"没有这个信息哦！");
                }else {
                    try {
                        String cookiceUser = getHeaderUser.getHader(response, request, "gg");
                        String ggId = JSONObject.parseObject(cookiceUser).getString("ggId");
                        // 插入签约
                        //ggSigningDaoMapper.addSignings()
                        ggSigningDaoMapper.addSignings(ggBusinessOpportunityDtaobject.getGgItemsToken(),ggBusinessOpportunityDtaobject.getGgBoName(),ggBusinessOpportunityDtaobject.getGgBoProvince(),ggBusinessOpportunityDtaobject.getGgBoCity(),ggBusinessOpportunityDtaobject.getGgBoDetailed(),ggBusinessOpportunityDtaobject.getGgBoHandle(),ggBusinessOpportunityDtaobject.getGgBoContact(),ggBusinessOpportunityDtaobject.getGgBoState(),ggBusinessOpportunityDtaobject.getGgBoLevel(),ggBusinessOpportunityDtaobject.getGgBoGender(),ggBusinessOpportunityDtaobject.getGgBoPosition(),ggBusinessOpportunityDtaobject.getGgBoTelephone(),ggBusinessOpportunityDtaobject.getGgBoFax(),ggBusinessOpportunityDtaobject.getGgBoMailbox(),ggBusinessOpportunityDtaobject.getGgBoQq(),ggBusinessOpportunityDtaobject.getGgBoWechat(),ggBusinessOpportunityDtaobject.getGgBoRemarks(),ggId,ggBusinessOpportunityDtaobject.getGgId(),ggBusinessOpportunityDtaobject.getGgBoTime(), ggBusinessOpportunityDtaobject.getGgBoAreaid());
                        // 从商机里面删除
                        ggBusinessOpportunityDaoMapper.delectGgBusinessOpportunityItems(id);
                        return return_format.return_format_All(0,"","");
                    }catch (Exception e){
                        return return_format.return_format_error(100002,"服务有问题了!");
                    }
                }
            }else {
                return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
            }

        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }

    }

    /**
     * 移除签约 移入 信息、商机
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object removeSignings(HttpServletResponse response, HttpServletRequest request, Integer id, Integer type) throws Exception {
        if(id != null && id > 0 && type != null) {
            // 获取合同信息
            GgSigningDataobject ggSigningDataobject = ggSigningDaoMapper.getSigningItems(id);
            // type 为1的时候 移入 信息
            if(type==1){
                // 移入信息
                ggInformationDaoMapper.addInformation(ggSigningDataobject.getGgItemsToken(), ggSigningDataobject.getGgSName(), ggSigningDataobject.getGgSProvince(), ggSigningDataobject.getGgSCity(), ggSigningDataobject.getGgSDetailed(), ggSigningDataobject.getGgSHandle(), ggSigningDataobject.getGgSContact(), ggSigningDataobject.getGgSState(), ggSigningDataobject.getGgSLevel(), ggSigningDataobject.getGgSGender(), ggSigningDataobject.getGgSPosition(), ggSigningDataobject.getGgSTelephone(), ggSigningDataobject.getGgSFax(), ggSigningDataobject.getGgSMailbox(), ggSigningDataobject.getGgSQq(), ggSigningDataobject.getGgSWechat(), ggSigningDataobject.getGgSRemarks(), ggSigningDataobject.getGgId(), ggSigningDataobject.getGgSTime(), ggSigningDataobject.getGgSAreaid(),ggSigningDataobject.getGgSZipcode(),ggSigningDataobject.getGgSWebsite(),ggSigningDataobject.getGgSSource());
                // 删除签约
                ggSigningDaoMapper.delectSigningsItems(id);
                return return_format.return_format_All(0,"","");
            }else if(type==2) { // type 为1的时候 移入 商机来的
                // 获取用户信息
                // 从cookice信息中过去用户信息 在这里只用要id就好
                String cookiceUser = getHeaderUser.getHader(response, request, "gg");
                String ggId = JSONObject.parseObject(cookiceUser).getString("ggId");
                // 插入签约
                ggBusinessOpportunityDaoMapper.yaddBusinessOpportunity(ggSigningDataobject.getGgItemsToken(), ggSigningDataobject.getGgSName(), ggSigningDataobject.getGgSProvince(), ggSigningDataobject.getGgSCity(), ggSigningDataobject.getGgSDetailed(), ggSigningDataobject.getGgSHandle(), ggSigningDataobject.getGgSContact(), ggSigningDataobject.getGgSState(), ggSigningDataobject.getGgSLevel(), ggSigningDataobject.getGgSGender(), ggSigningDataobject.getGgSPosition(), ggSigningDataobject.getGgSTelephone(), ggSigningDataobject.getGgSFax(), ggSigningDataobject.getGgSMailbox(), ggSigningDataobject.getGgSQq(), ggSigningDataobject.getGgSWechat(), ggSigningDataobject.getGgSRemarks(), ggSigningDataobject.getGgId(),ggId, ggSigningDataobject.getGgSTime(), ggSigningDataobject.getGgSAreaid(),ggSigningDataobject.getGgSZipcode(),ggSigningDataobject.getGgSWebsite(),ggSigningDataobject.getGgSSource());
                // 删除签约
                ggSigningDaoMapper.delectSigningsItems(id);
                return return_format.return_format_All(0,"","");
            }else {
                return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
            }

        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    /**
     * 查询 列表
     */
    @Override
    public Object getSigning(HttpServletResponse response, HttpServletRequest request, Integer page, Integer pageNumber, String name, Integer type, Integer state, String remarks) throws Exception {
        if(page != null && page > 0 && pageNumber != null && pageNumber > 0 ){
            // 获取用户信息
            // 从cookice信息中过去用户信息 在这里只用要id就好
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
            // 数据列表
            name = name!=null && name.equals("") ? null:name;
            type = type!=null && type.equals("") ? null:type;
            state = state!=null && state.equals("") ? null:state;
            remarks = remarks!=null && remarks.equals("") ? null:remarks;
            Integer SigningSelectCountNumber = ggSigningDaoMapper.getSigningSelectCount(ggIdUser,name,type,state,remarks);
            List<GgSigningDataobject> getSigningSelectList = ggSigningDaoMapper.getSigningSelectList((page-1)*pageNumber, pageNumber,ggIdUser,name,type,state,remarks);
            return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(SigningSelectCountNumber/pageNumber),pageNumber,SigningSelectCountNumber,getSigningSelectList),"");

        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    /**
     * 删除签约
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object delectSignings(HttpServletResponse response, HttpServletRequest request, Integer id) throws Exception {
        if(id != null || id<=0 ){
            GgSigningDataobject ggSigningDataobject = ggSigningDaoMapper.getSigningItems(id);// 获取商机信息
            if(ggSigningDataobject == null){
                return return_format.return_format_error(100002,"删除商机不存在！");
            }else {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
                // 移入删除信息
                ggDeletcInformationService.addDeletcInformationService(ggSigningDataobject.getGgItemsToken(), ggSigningDataobject.getGgSName(), ggSigningDataobject.getGgSProvince(), ggSigningDataobject.getGgSCity(), ggSigningDataobject.getGgSDetailed(), ggSigningDataobject.getGgSHandle(), ggSigningDataobject.getGgSContact(), ggSigningDataobject.getGgSState(), ggSigningDataobject.getGgSLevel(), ggSigningDataobject.getGgSGender(), ggSigningDataobject.getGgSPosition(), ggSigningDataobject.getGgSTelephone(), ggSigningDataobject.getGgSFax(), ggSigningDataobject.getGgSMailbox(), ggSigningDataobject.getGgSQq(), ggSigningDataobject.getGgSWechat(), ggSigningDataobject.getGgSRemarks(), ggSigningDataobject.getGgId(), ggIdUser, ggSigningDataobject.getGgSTime(), ggIdUser, ggSigningDataobject.getGgSAreaid(),ggSigningDataobject.getGgSZipcode(),ggSigningDataobject.getGgSWebsite(),ggSigningDataobject.getGgSSource());
                // 删除签约
                ggSigningDaoMapper.delectSigningsItems(id);
                return return_format.return_format_All(0,"","");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    /**
     * 增加签约
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object addSigningsAll(HttpServletResponse response, HttpServletRequest request, String name, String sex, String position, String phone, String mailbox, String qq, String weChat, String provid, String cityid, String areaid, String address, Integer level, Integer state, String remarks, String username, String fax, String contact) throws Exception {

        if(name != null && name != "" && username != null && username != "" && address != null && address != "" && phone!=null  && phone != ""){
            try {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
                // 弄一个商机、信息公共唯一标识符
                GgItemsOnlyDataobject ggItemsOnlyDataobjectToken = ggItemsOnlyService.judgeItemsOnly(response,request,"签约");
                if (ggItemsOnlyDataobjectToken != null){
                    ggSigningDaoMapper.addSigningsItems(name,provid,cityid,areaid,address,contact,username,state,level,sex,position,phone,fax,mailbox,qq,weChat,remarks,ggIdUser,ggIdUser,ggItemsOnlyDataobjectToken.getGgIoOnly());
                    return return_format.return_format_All(0,"","");
                }else {
                    return return_format.return_format_error(100003,"插入失败请稍后重试！");
                }
            }catch (Exception e){
                return return_format.return_format_error(100003,"插入失败请稍后重试！");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    /**
     * 查看签约详情
     */
    @Override
    public Object getSigningsSelectItems(HttpServletResponse response, HttpServletRequest request, String token) throws IOException {
        if(token != null){
            try {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
                return return_format.return_format_All(0,ggSigningDaoMapper.getSigningSelectItems(ggIdUser,token),"");
            }catch (Exception e){
                return return_format.return_format_error(100003,"服务有问题了");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    /**
     * 修改签约
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object updateSignings(HttpServletResponse response, HttpServletRequest request, String ggItemsToken, String ggBoName, String ggBoProvince, String ggBoCity, String ggBoAreaid, String ggBoDetailed, String ggBoHandle, String ggBoContact, Integer ggBoState, Integer ggBoLevel, String ggBoGender, String ggBoPosition, String ggBoTelephone, String ggBoFax, String ggBoMailbox, String ggBoQq, String ggBoWechat, String ggSZipcode, String ggSWebsite, String ggSSource) throws Exception {
        if(ggBoName != null || ggBoName != "" || ggBoContact != null || ggBoContact != "" || ggBoTelephone != null || ggBoTelephone != "" || ggBoDetailed!=null  || ggBoDetailed != ""){
            try {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
                ggSigningDaoMapper.updateSigning(ggItemsToken, ggBoName, ggBoProvince, ggBoCity, ggBoAreaid, ggBoDetailed, ggBoHandle ,ggBoContact, ggBoState ,ggBoLevel ,ggBoGender ,ggBoPosition ,ggBoTelephone ,ggBoFax ,ggBoMailbox ,ggBoQq, ggBoWechat, ggIdUser , ggSZipcode, ggSWebsite, ggSSource);
                return return_format.return_format_All(0,"","");
            }catch (Exception e){
                return return_format.return_format_error(100002,"服务有问题了！");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }

    }

    /**
     * 签约批量  移除 商机移入信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object removebuSigningsAll(HttpServletResponse response, HttpServletRequest request, String listid, Integer type) throws Exception{
        try {
            List id_list = JSON.parseObject(listid,new TypeReference<List>(){});
            // 获取用户信息
            // 从cookice信息中过去用户信息 在这里只用要id就好
            String cookiceUser = getHeaderUser.getHader(response, request, "gg");
            String ggId = JSONObject.parseObject(cookiceUser).getString("ggId");
            if(id_list.size()!=0){
                Integer yes = 0;  // 移动成功
                Integer not = 0;  // 没有这个信息
                Integer error = 0; // 错误
                for (int i = 0;i < id_list.size(); i++){
                    try {
                        Integer id = Integer.parseInt(String.valueOf(id_list.get(i))) ;// id
                        GgSigningDataobject ggSigningDataobject = ggSigningDaoMapper.getSigningItems(id);
                        if(ggSigningDataobject ==null){
                            not = not + 1;
                        }else {
                            // type 为1的时候 移入 信息
                            if(type==1){
                                // 移入信息
                                ggInformationDaoMapper.addInformation(ggSigningDataobject.getGgItemsToken(), ggSigningDataobject.getGgSName(), ggSigningDataobject.getGgSProvince(), ggSigningDataobject.getGgSCity(), ggSigningDataobject.getGgSDetailed(), ggSigningDataobject.getGgSHandle(), ggSigningDataobject.getGgSContact(), ggSigningDataobject.getGgSState(), ggSigningDataobject.getGgSLevel(), ggSigningDataobject.getGgSGender(), ggSigningDataobject.getGgSPosition(), ggSigningDataobject.getGgSTelephone(), ggSigningDataobject.getGgSFax(), ggSigningDataobject.getGgSMailbox(), ggSigningDataobject.getGgSQq(), ggSigningDataobject.getGgSWechat(), ggSigningDataobject.getGgSRemarks(), ggSigningDataobject.getGgId(), ggSigningDataobject.getGgSTime(), ggSigningDataobject.getGgSAreaid(),ggSigningDataobject.getGgSZipcode(),ggSigningDataobject.getGgSWebsite(),ggSigningDataobject.getGgSSource());
                                // 删除签约
                                ggSigningDaoMapper.delectSigningsItems(id);
                                yes = yes + 1;
                            }else if(type==2) { // type 为1的时候 移入 商机来的
                                // 插入签约
                                ggBusinessOpportunityDaoMapper.yaddBusinessOpportunity(ggSigningDataobject.getGgItemsToken(), ggSigningDataobject.getGgSName(), ggSigningDataobject.getGgSProvince(), ggSigningDataobject.getGgSCity(), ggSigningDataobject.getGgSDetailed(), ggSigningDataobject.getGgSHandle(), ggSigningDataobject.getGgSContact(), ggSigningDataobject.getGgSState(), ggSigningDataobject.getGgSLevel(), ggSigningDataobject.getGgSGender(), ggSigningDataobject.getGgSPosition(), ggSigningDataobject.getGgSTelephone(), ggSigningDataobject.getGgSFax(), ggSigningDataobject.getGgSMailbox(), ggSigningDataobject.getGgSQq(), ggSigningDataobject.getGgSWechat(), ggSigningDataobject.getGgSRemarks(), ggSigningDataobject.getGgId(),ggId, ggSigningDataobject.getGgSTime(), ggSigningDataobject.getGgSAreaid(),ggSigningDataobject.getGgSZipcode(),ggSigningDataobject.getGgSWebsite(),ggSigningDataobject.getGgSSource());
                                // 删除签约
                                ggSigningDaoMapper.delectSigningsItems(id);
                                yes = yes + 1;
                            }else {
                                not = not + 1;
                            }
                        }
                    }catch (Exception e){
                        error = error + 1;
                    }
                }
                Map dataMap = new HashMap();
                dataMap.put("yes", yes);
                dataMap.put("not", not);
                dataMap.put("error", error);
                return return_format.return_format_All(0, dataMap,"");
            }else {
                return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
            }
        }catch (Exception e){
            return return_format.return_format_error(100002,"服务有问题了！");
        }
    }

    /**
     * 签约批量  删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object delectbuSigningsAll(HttpServletResponse response, HttpServletRequest request, String listid) throws Exception {
        try {
            List id_list = JSON.parseObject(listid,new TypeReference<List>(){});
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
            if(id_list.size()!=0){
                try {
                    Integer yes = 0;  // 移动成功
                    Integer not = 0;  // 没有这个信息
                    Integer error = 0; // 错误
                    for (int i = 0;i < id_list.size(); i++){
                        try {
                            Integer id = Integer.parseInt(String.valueOf(id_list.get(i))) ;// id
                            GgSigningDataobject ggSigningDataobject = ggSigningDaoMapper.getSigningItems(id);// 获取商机信息
                            if(ggSigningDataobject ==null){
                                not = not + 1;
                            }else {
                                // 移入删除信息
                                ggDeletcInformationService.addDeletcInformationService(ggSigningDataobject.getGgItemsToken(), ggSigningDataobject.getGgSName(), ggSigningDataobject.getGgSProvince(), ggSigningDataobject.getGgSCity(), ggSigningDataobject.getGgSDetailed(), ggSigningDataobject.getGgSHandle(), ggSigningDataobject.getGgSContact(), ggSigningDataobject.getGgSState(), ggSigningDataobject.getGgSLevel(), ggSigningDataobject.getGgSGender(), ggSigningDataobject.getGgSPosition(), ggSigningDataobject.getGgSTelephone(), ggSigningDataobject.getGgSFax(), ggSigningDataobject.getGgSMailbox(), ggSigningDataobject.getGgSQq(), ggSigningDataobject.getGgSWechat(), ggSigningDataobject.getGgSRemarks(), ggSigningDataobject.getGgId(), ggIdUser, ggSigningDataobject.getGgSTime(), ggIdUser, ggSigningDataobject.getGgSAreaid(),ggSigningDataobject.getGgSZipcode(),ggSigningDataobject.getGgSWebsite(),ggSigningDataobject.getGgSSource());
                                // 删除签约
                                ggSigningDaoMapper.delectSigningsItems(id);
                                yes = yes + 1;
                            }
                        }catch (Exception e){
                            error = error + 1;
                        }
                    }
                    Map dataMap = new HashMap();
                    dataMap.put("yes", yes);
                    dataMap.put("not", not);
                    dataMap.put("error", error);
                    return return_format.return_format_All(0, dataMap,"");
                }catch (Exception e){
                    return return_format.return_format_error(100002,"服务有问题了！");
                }
            }else {
                return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
            }
        }catch (Exception e){
            return return_format.return_format_error(100002,"服务有问题了！");
        }
    }
}
